package com.zyl.courseservice.controller;

/**
 * Auther:郑誉略
 * Date: 2020-12-08 9:48
 * Description: <描述>
 */
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zyl.commonutils.ResponseResult;
import com.zyl.courseservice.entity.TClasses;
import com.zyl.courseservice.service.TClassesService;
import com.zyl.commonbase.handle.CourseException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(  description = "班级管理")
@RestController
@RequestMapping({"/classes"})
@CrossOrigin
public class TClassesController {

    @Autowired
    private TClassesService tClassesService;


    @ApiOperation("所有班级列表")
    @GetMapping({"findAll"})
    public ResponseResult findAllClass() {
        List list = this.tClassesService.list((Wrapper)null);
        return ResponseResult.ok().data("items", list);
    }

    @ApiOperation("分页查询班级列表")
    @GetMapping({"pageClass/{current}/{limit}"})
    public ResponseResult pageListClass(@PathVariable long current, @PathVariable long limit) {
        Page pageClass = new Page(current, limit);
        this.tClassesService.page(pageClass, (Wrapper)null);
        long total = pageClass.getTotal();
        List records = pageClass.getRecords();
        return ResponseResult.ok().data("total", Long.valueOf(total)).data("rows", records);
    }

    @ApiOperation("新增班级")
    @PostMapping({"/addClass/{value}"})
    public ResponseResult addClass(@PathVariable String value) throws CourseException {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("classes_name", value);
        TClasses t = (TClasses)this.tClassesService.getOne(wrapper);
        if(null != t) {
            System.out.println(t);
            throw new CourseException(20001, "班级名称已存在");
        } else {
            TClasses tclass = new TClasses();
            tclass.setClassesName(value);
            System.out.println(tclass);
            boolean save = this.tClassesService.save(tclass);
            return save? ResponseResult.ok(): ResponseResult.error();
        }
    }

    @ApiOperation("根据ID删除班级")
    @DeleteMapping({"{id}"})
    public ResponseResult removeById(@ApiParam(
            name = "id",
            value = "班级ID",
            required = true
    ) @PathVariable int id) {
        boolean flag = this.tClassesService.removeById(id);
        return flag? ResponseResult.ok(): ResponseResult.error();
    }

    @ApiOperation("根据ID修改班级")
    @PostMapping({"updateClass"})
    public ResponseResult updateClass(@ApiParam(
            name = "class",
            value = "班级对象",
            required = true
    ) @RequestBody TClasses classes) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("classes_name", classes.getClassesName());
        System.out.println(classes);
        TClasses t = (TClasses)this.tClassesService.getOne(wrapper);
        if(null != t) {
            System.out.println(t);
            throw new CourseException(Integer.valueOf(20001), "班级名称已存在");
        } else {
            boolean flag = this.tClassesService.updateById(classes);
            return flag? ResponseResult.ok(): ResponseResult.error();
        }
    }

    @ApiOperation("带条件的分页查询")
    @PostMapping({"pageQuery/{current}/{limit}"})
    public ResponseResult pageQuery(@PathVariable long current, @PathVariable long limit, @RequestBody(
            required = false) TClasses classes) {
        Page pageclass = new Page(current, limit);
        QueryWrapper wrapper = new QueryWrapper();
        String name = classes.getClassesName();
        if(!StringUtils.isEmpty(name)) {
            wrapper.like("classes_name", name);
        }

        wrapper.orderByDesc(new String[]{"create_time"});
        this.tClassesService.page(pageclass, wrapper);
        long total = pageclass.getTotal();
        List records = pageclass.getRecords();
        return ResponseResult.ok().data("total", total).data("rows", records);
    }

    @ModelAttribute("classData")
    public ResponseResult loadclasses() {
        List classes = this.tClassesService.list((Wrapper)null);
        return ResponseResult.ok().data("item", classes);
    }

    @ResponseBody
    @GetMapping({"{id}"})
    public ResponseResult getClassInfoById(@PathVariable int id) {
        TClasses tcl = (TClasses)this.tClassesService.getById(id);
        return ResponseResult.ok().data("class", tcl);
    }
}

